<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\1\6 0006
 * Time: 10:54
 */
namespace app\admin\model;
use think\Model;
class Version extends Model{

    /**
     * 获取更新状态
     * @author 王柯 <wangke.email@foxmail.com>
     * @return mixed
     */
    public function getIsForceAttr($value){
        $is_update = [
            null => null,
            0 => '否',
            1 => '是',
        ];
        return $is_update[$value];
    }

    /**
     * 检测更新
     * @param $app_type string app类型 *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return mixed
     */
    public function check_update($app_type = '') {
        $data = [
            'app_type' => strtolower($app_type),
        ];

        $order = [
            'id' => 'desc',
        ];

        return $this->where($data)
            ->order($order)
            ->limit(1)
            ->find();
    }

    /**
     * 获取所有更新版本信息
     * @author 王柯 <wangke.email@foxmail.com>
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getAll($where = ''){
        $data = $this
            ->where($where)
            ->select();
        if($data){
            foreach ($data as $key => $val){
                $data[$key] = $val->toArray();
                $data[$key]['is_force'] = model('Version')->get($val['id'])->is_force;
            }
        }

        return $data;
    }

    /**
     * 获取单条版本更新信息详情
     * @author 王柯 <wangke.email@foxmail.com>
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getOne($id){

        $data = $this
            ->where('id='.$id)
            ->find();
        if($data){
            $data = $data->toArray();
        }

        return $data;
    }

    /**
     * 获取更新版本记录总数
     * @author 王柯 <wangke.email@foxmail.com>
     * @return int|string
     */
    public function getCount($where = ''){
        $count = $this
            ->where($where)
            ->count();
        return $count;
    }
}